#5.使用棋盘格及自选风景图像，分别使用SIFT、FAST及ORB算子检测角点，并比较分析检测结果。 
#(可选)使用Harris角点检测算子检测棋盘格，并与上述结果比较。 
#角点：是一类含有足够信息且能从当前帧和下一帧中都能提取出来的点。
#SIFT：该算法用于检测斑点；确切地说 opencv-contrib-python 3.4.3.18 之前是有效的
#FAST：该算法用于检测角点；
#ORB：该算法代表带方向的FAST算法与具有旋转不变性的BRIEF算法；

#Harris：该算法用于检测角点；
#SURF：该算法用于检测角点；
#BRIEF：该算法用于检测斑点；


import cv2
import numpy as np
from matplotlib import pyplot as plt

###************************************************************************
img_qipan_SIFT= cv2.imread('qipan.jpg')
gray = cv2.cvtColor(img_qipan_SIFT, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptor = sift.detectAndCompute(gray, None)
img_qipan_SIFT01 = cv2.drawKeypoints(image=img_qipan_SIFT, outImage=img_qipan_SIFT, keypoints=keypoints, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,color=(0, 0, 255))
cv2.imshow('img_qipan_SIFT', img_qipan_SIFT01)
###-------------------------------------------------------------------
img_fengjing_SIFT = cv2.imread('fengjing.jpg')
gray_large = cv2.cvtColor(img_fengjing_SIFT, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptor = sift.detectAndCompute(gray, None)
keypoints2, descriptor2 = sift.detectAndCompute(gray_large, None)
img_fengjing_SIFT01 = cv2.drawKeypoints(image=img_fengjing_SIFT, outImage=img_fengjing_SIFT, keypoints=keypoints2,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,color=(0, 0, 255))
cv2.imshow('img_fengjing_SIFT', img_fengjing_SIFT01)
###************************************************************************

#fast
img_qipan_fast = cv2.imread("qipan.jpg", 0)
#fast = cv2.FastFeatureDetector_create(threshold=10)
fast = cv2.FastFeatureDetector_create()
keypoints = fast.detect(img_qipan_fast, None)
img_qipan_fast01 = cv2.drawKeypoints(img_qipan_fast, keypoints, None, (0, 0, 255))
cv2.imshow('img_qipan_fast', img_qipan_fast01)
###-------------------------------------------------------------------
img_fengjing_fast = cv2.imread("fengjing.jpg", 0)
#fast = cv2.FastFeatureDetector_create(threshold=10)
fast = cv2.FastFeatureDetector_create()
keypoints = fast.detect(img_fengjing_fast, None)
img_fengjing_fast01 = cv2.drawKeypoints(img_fengjing_fast, keypoints, None, (0, 0, 255))
cv2.imshow('img_fengjing_fast', img_fengjing_fast01)
###************************************************************************


#Harris：

# 读入图像并转化为float类型，用于传递给harris函数
img1 = cv2.imread('qipan.jpg')
gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray_img1 = np.float32(gray_img1)
# 对图像执行harris
Harris_detector = cv2.cornerHarris(gray_img1, 2, 3, 0.04)
# 腐蚀harris结果
dst = cv2.dilate(Harris_detector, None)
# 设置阈值
thres = 0.01 * dst.max()
img1[dst > thres] = [255, 0, 0]
cv2.imshow('qipan_Harris', img1)
###-------------------------------------------------------------------
img1 = cv2.imread('fengjing.jpg')
gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray_img1 = np.float32(gray_img1)
# 对图像执行harris
Harris_detector = cv2.cornerHarris(gray_img1, 2, 3, 0.04)
# 腐蚀harris结果
dst = cv2.dilate(Harris_detector, None)
# 设置阈值
thres = 0.01 * dst.max()
img1[dst > thres] = [255, 0, 0]
cv2.imshow('fengjing_Harris', img1)

###************************************************************************
#OBR
img1 = cv2.imread('qipan.jpg',0)
img2 = cv2.imread('fengjing.jpg',0)

# 使用ORB特征检测器和描述符，计算关键点和描述符
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)

bf = cv2.BFMatcher(normType=cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)

img3 = cv2.drawMatches(img1=img1,keypoints1=kp1,
                       img2=img2,keypoints2=kp2,
                       matches1to2=matches,
                       outImg=img2, flags=2)
plt.imshow(img3)
plt.show()
###************************************************************************
cv2.waitKey(0)
cv2.destroyAllWindows()